package com.zoroki.hmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoroki.hmusic.entity.*;
import com.zoroki.hmusic.es.EsPlaylistRepository;
import com.zoroki.hmusic.mapper.FavoritePlaylistMapper;
import com.zoroki.hmusic.mapper.PlaylistMapper;
import com.zoroki.hmusic.mapper.PlaylistSongMapper;
import com.zoroki.hmusic.mapper.SongMapper;
import com.zoroki.hmusic.service.IFavoritePlaylistService;
import com.zoroki.hmusic.service.IPlaylistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoroki.hmusic.service.IPlaylistSongService;
import com.zoroki.hmusic.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zoroki
 * @since 2023-05-10
 */
@Service
@Slf4j
public class PlaylistServiceImpl extends ServiceImpl<PlaylistMapper, Playlist> implements IPlaylistService {

    @Autowired
    private PlaylistMapper playlistMapper;

    @Autowired
    IFavoritePlaylistService favoritePlaylistService;


    @Autowired
    private EsPlaylistRepository esPlaylistRepository;

    @Override
    public List<Playlist> findPlaylistByUserId(int userId) {
        QueryWrapper<Playlist> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return playlistMapper.selectList(wrapper);
    }

    @Override
    public int addPlaylist(Playlist playlist) {
        int insert = playlistMapper.insert(playlist);
        if (insert > 0) {
            Playlist newPlaylist = playlistMapper.selectById(playlist.getId());
            try{
                esPlaylistRepository.save(newPlaylist);
            }catch (Exception e){
                log.error("esPlaylist添加失败:{}",e.getMessage());
                return -1;
            }
            return 1;
        }
        return -1;
    }

    @Override
    public Playlist findPlaylistById(int id) {
        return playlistMapper.selectById(id);
    }


    @Autowired
    IPlaylistSongService playlistSongService;
    @Override
    public int deletePlaylistById(int id) {
        //从playlistSong中删除与之关联的数据
        playlistSongService.deletePlaylistSongByPlaylistId(id);
        log.info("playlistSong删除成功:id = {}", id);
        //从收藏中删除关联数据
        favoritePlaylistService.deleteFavoritePlaylistByPlaylistId(id);
        log.info("favoritePlaylist删除成功:id = {}", id);
        //从es中删除数据
        esPlaylistRepository.deleteById(String.valueOf(id));
        log.info("esPlaylist删除成功:id = {}", id);

        return playlistMapper.deleteById(id);
    }

    @Override
    public SearchHits<Playlist> findPlaylistByKeyword(String keyword) {
        return esPlaylistRepository.findPlaylistByNameOrRemark(keyword, keyword);
    }

    @Override
    public Page<Playlist> getPlaylistPageByKeyword(String keyword, int pageIndex) {
        return esPlaylistRepository.findPlaylistsByNameOrRemark(keyword,keyword, PageRequest.of(pageIndex,6, Sort.by(Sort.Order.asc("createdAt"))));
    }

    @Override
    public int updatePlaylist(Playlist playlist) {
        int i = playlistMapper.updateById(playlist);
        esPlaylistRepository.save(playlist);
        return i;
    }

    @Override
    public Playlist findPlaylistByIdAndUserId(int id, int userId) {
        QueryWrapper<Playlist> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("user_id", userId);

        return playlistMapper.selectOne(wrapper);
    }
}
